package org.springframework.data.redis.core;

import com.google.common.base.Preconditions;

/**
 * @Auther: syh
 * @Date: 2020/7/24
 * @Description:
 */
public class JedisBloomFilterBuilder<K, V> {

    private K key;
    private double errorRate;
    private long initCapacity;
    BloomOperations<? extends K, ? extends V> operations;

    public static JedisBloomFilterBuilder<Object, Object> newBuilder() {
        return new JedisBloomFilterBuilder<>();
    }

    public JedisBloomFilterBuilder<K, V> errorRate(double errorRate) {
        this.errorRate = errorRate;
        return this;
    }

    public JedisBloomFilterBuilder<K, V> initCapacity(long initCapacity) {
        this.initCapacity = initCapacity;
        return this;
    }

    public <K1 extends K> JedisBloomFilterBuilder<K, V> key(K1 key) {
        this.key = key;
        return this;
    }

    public <K1 extends K, V1 extends V> JedisBloomFilterBuilder<K, V> operations(BloomOperations<K1, V1> operations) {
        this.operations = operations;
        return this;
    }

    public <K1 extends K, V1 extends V> JedisBloomFilter<K1, V1> build() {
        Preconditions.checkNotNull(this.key);
        Preconditions.checkArgument(
                this.initCapacity >= 0, "Expected insertions (%s) must be >= 0", initCapacity);
        Preconditions.checkArgument(
                this.errorRate > 0.0, "False positive probability (%s) must be > 0.0", errorRate);
        Preconditions.checkArgument(
                this.errorRate < 1.0, "False positive probability (%s) must be < 1.0", errorRate);
        return new JedisBloomFilter(key, errorRate, initCapacity, this.operations);
    }
}
